﻿using System;
using BusinessLogic.ClassFactory;
using Common;
using DataAccess;
using System.Web.UI;
using ValidateCommon;
using Util.PageInfo;
using System.Globalization;
using System.Data;

namespace RichECMS.ServerPages.Links.LinkEditor
{
    public partial class Update : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["id"] != null)
                {
                    if (ParamAuth.IsNumber(Request["id"].ToString().Trim()))
                    {
                        aBack.HRef += "?Gid=" + State.intLinkGroupID;
                        LitPaagePath.Text = PageInfo.ShowLinkGroupName(State.intLinkGroupID);
                        LitPageName.Text = LitPaagePath.Text;
                        InitDataBind(Convert.ToInt32(Request["id"].ToString().Trim()));
                    }
                    else
                    {
                        Alert("参数错误");
                    }
                }
                else
                {
                    Alert("参数错误");
                }
            }
        }

        /// <summary>
        /// 展示数据
        /// </summary>
        /// <param name="intArticleId">文章ID</param>
        private void InitDataBind(int intLinksID)
        {
            try
            {
                using (LinksInfoFactory objLinksInfoFactory = new LinksInfoFactory())
                {
                    BusinessLogic.BusinessRules.LinksInfo objLinksInfo = objLinksInfoFactory.Creator(intLinksID);
                    if (objLinksInfo != null)
                    {
                        DataTable tblTmp = objLinksInfo.Data.Tables[0];
                        DataRow row = tblTmp.Rows[0];
                        if (row["chvPicUrl"] != DBNull.Value)
                        {
                            this.ImgShow.Src = objLinksInfo.DataItem.chvPicUrl.Trim().Length == 0
                                                    ? "../../../sysCommon/sysImages/systerm/test1.jpg"
                                                    : "/pcfiles/" + objLinksInfo.DataItem.chvPicUrl;
                        }
                        else
                        {
                            this.ImgShow.Src = "../../../sysCommon/sysImages/systerm/test1.jpg";
                            Imghidden.Value = "";
                        }
                        
                        this.txtTitle.Value = objLinksInfo.DataItem.chvName;
                        this.txtdescription.Value = objLinksInfo.DataItem.chvUrl;
                        if (row["chvUrl"] != DBNull.Value)
                        {
                            this.txtdescription.Value = objLinksInfo.DataItem.chvUrl;
                        }
                        else
                        {
                            this.txtdescription.Value ="";
                        }
                        this.txtOrder.Value = objLinksInfo.DataItem.intSort.ToString(CultureInfo.InvariantCulture);
                        if (objLinksInfo.DataItem.intVerify == 1)
                        {
                            CkboxPublish.Checked = true;
                        }
                        if (objLinksInfo.DataItem.intGlobalRec == 1)
                        {
                            CkboxIsGlobalRec.Checked = true;
                        }
                        if (objLinksInfo.DataItem.intGlobalTop == 1)
                        {
                            CkboxIsGlobalTop.Checked = true;
                        }
                    }
                    else
                    {
                        Alert("该文章不存在");
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 验证输入
        /// </summary>
        private bool ValidateValues()
        {
            bool bolIsValidate = true;
            if (this.txtTitle.Value.Trim().Length == 0)
            {
                Alert("请填写标题");
                bolIsValidate = false;
            }

            if (!ParamAuth.IsNumber(SqlCommon.SqlStringFilter(this.txtOrder.Value)))
            {
                Alert("排序请输入数字");
                bolIsValidate = false;
            }
            return bolIsValidate;
        }

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SubmitBtnHander(object sender, ImageClickEventArgs e)
        {
            if (!ValidateValues())
            {
                return;
            }
            using (DBManager objDBManager = new DBManager())
            {
                objDBManager.BeginTransaction();
                try
                {

                    LinksInfoFactory objLinksInfoFactory = new LinksInfoFactory(objDBManager);
                    BusinessLogic.BusinessRules.LinksInfo objLinksInfo = objLinksInfoFactory.Creator(Convert.ToInt32(Request["id"].ToString().Trim()));
                    //图片地址
                    if (Imghidden.Value.Length != 0)
                    {
                        objLinksInfo.DataItem.chvPicUrl = Imghidden.Value;

                        string strOlnyName = Imghidden.Value.Remove(Imghidden.Value.LastIndexOf('.'));
                        string strExtensionName = Imghidden.Value.Remove(0, Imghidden.Value.LastIndexOf('.'));
                    }
                    else
                    {
                        objLinksInfo.DataItem.chvPicUrl = "../../../sysCommon/sysImages/systerm/test1.jpg";
                    }
                    objLinksInfo.DataItem.chvName = this.txtTitle.Value;
                    objLinksInfo.DataItem.chvUrl = this.txtdescription.Value;
                    objLinksInfo.DataItem.intSort = int.Parse(this.txtOrder.Value);
                    objLinksInfo.DataItem.intGroupID = State.intLinkGroupID;
                    objLinksInfo.DataItem.intVerify = CkboxPublish.Checked == true ? 1 : 0;
                    objLinksInfo.DataItem.intGlobalRec = CkboxIsGlobalRec.Checked == true ? 1 : 0;
                    objLinksInfo.DataItem.intGlobalTop = CkboxIsGlobalTop.Checked == true ? 1 : 0;

                    objLinksInfo.Update();

                    //修改栏目状态
                    LinksGroupFactory objLinksGroupFactory = new LinksGroupFactory(objDBManager);
                    BusinessLogic.BusinessRules.LinksGroup objLinksGroup = objLinksGroupFactory.Creator(State.intLinkGroupID);
                    objLinksGroup.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;

                    objLinksGroup.Update();

                    //FindparentSection(State.intSTID);
                    //提交
                    objDBManager.Commit();
                    objLinksGroupFactory.Dispose();
                    objLinksInfoFactory.Dispose();
                }
                catch (Exception ex)
                {
                    Alert(ex.ToString().Replace("\r\n", " "));
                    objDBManager.Rollback();
                    return;
                }
            }
            AlertAndRedirect("修改成功！", "../LinksList.aspx?Gid=" + State.intLinkGroupID.ToString(), false);
        }
    }
}